package cn.matezk.lianlian.beans;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class NotifyConfig {
    private List<String> mobile;
    private List<Event> events;
    private List<Contacts> contacts;

    @Data
    public static class Event {
        private String eventId;
        private Boolean wechatEnable = Boolean.FALSE;
        private Boolean smsEnable = Boolean.FALSE;
        private Boolean voiceEnable = Boolean.FALSE;
        // 是否重复推送
        private Boolean repeatEnable = Boolean.FALSE;
        // 推送间隔(分钟)
        private Integer repeatInterval = 5;
        // 推送次数
        private Integer repeatTimes = 2;
    }

    @Data
    public static class Contacts {
        private String name;
        private String mobile;
    }

    public List<String> getMobile() {
        if (CollectionUtil.isEmpty(contacts)) return mobile;
        List<String> phoneNumber = new ArrayList<>();
        for (Contacts c : contacts) {
            phoneNumber.add(c.getMobile());
        }
        return phoneNumber;
    }

    public Boolean wechatEnable(String eventId) {
        if (ObjectUtil.isEmpty(events)) return false;
        if ("notify".equals(eventId))   return true;
        for (Event e : events) {
            if (ObjectUtil.equals(eventId, e.getEventId())) {
                return e.getWechatEnable();
            }
        }
        return false;
    }

    public Boolean smsEnable(String eventId) {
        if (ObjectUtil.isEmpty(events)) return false;
        for (Event e : events) {
            if (ObjectUtil.equals(eventId, e.getEventId())) {
                return e.getSmsEnable();
            }
        }
        return false;
    }

    public Boolean voiceEnable(String eventId) {
        if (ObjectUtil.isEmpty(events)) return false;
        for (Event e : events) {
            if (ObjectUtil.equals(eventId, e.getEventId())) {
                return e.getVoiceEnable();
            }
        }
        return false;
    }

    public Event matchEvent(String eventId) {
        if (ObjectUtil.isEmpty(events)) return null;
        for (Event event : events) {
            if (ObjectUtil.equals(eventId, event.getEventId()))
                return event;
        }
        return null;
    }
}
